package com.sojson.result;

import com.github.pagehelper.Page;
import com.sojson.config.exception.TransErrorCode;
import com.sojson.config.exception.TransException;
import com.sojson.result.bean.PageParam;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class Result<T> {

    @ApiModelProperty(value = "响应码", position = 0)
    private Integer code;
    @ApiModelProperty(value = "响应信息", position = 1)
    private String message;
    @ApiModelProperty(value = "主要数据", position = 2)
    private T data;
    @ApiModelProperty(value = "分页数据", position = 4)
    private Object other;

    public static <T> Result<T> success() {
        TransErrorCode success = TransErrorCode.SUCCESS;
        return success(success.getCode(), success.getMsg(), null, null);
    }

    public static <T> Result<T> successMeg(String message) {
        return success(TransErrorCode.SUCCESS.getCode(), message, null, null);
    }

    public static <T> Result<T> success(String message, T data) {
        return success(TransErrorCode.SUCCESS.getCode(), message, data, null);
    }

    public static <T> Result<T> success(String message, T data, T other) {
        return success(TransErrorCode.SUCCESS.getCode(), message, data, other);
    }

    public static <T> Result<T> success(Integer code, String message, T data, T other) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        result.setOther(page(other));
        return result;
    }

    public static <T> Result<T> success(TransException transException, T data, T other) {
        return success(transException.getErrCode(), transException.getErrMsg(), data, other);
    }

    public static <T> Result<T> success(T data) {
        if (data instanceof Page) {
            return success(TransErrorCode.SUCCESS, data, data);
        } else {
            return success(TransErrorCode.SUCCESS, data, null);
        }
    }

    public static <T> Result<T> success(T data, T other) {
        return success(TransErrorCode.SUCCESS, data, other);
    }

    public static <T> Result<T> success(TransErrorCode transErrorCode, T data, T other) {
        return success(transErrorCode.getCode(), transErrorCode.getMsg(), data, other);
    }

    public static <T> Result<T> fail() {
        TransErrorCode fail = TransErrorCode.FAIL;
        return fail(fail.getCode(), fail.getMsg(), null, null);
    }

    public static <T> Result<T> fail(String message) {
        return fail(TransErrorCode.FAIL.getCode(), message, null, null);
    }

    public static <T> Result<T> fail(Integer code, String message) {
        return fail(code, message, null, null);
    }

    public static <T> Result<T> fail(Integer code, String message, T data) {
        return fail(code, message, data, null);
    }

    public static <T> Result<T> fail(Integer code, String message, T data, T other) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        result.setOther(other);
        return result;
    }

    public static <T> Result<T> fail(TransException transException) {
        return fail(transException.getErrCode(), transException.getErrMsg(), null, null);
    }

    public static <T> Result<T> fail(TransException transException, T data) {
        return fail(transException.getErrCode(), transException.getErrMsg(), data, null);
    }

    public static <T> Result<T> fail(TransException transException, T data, T other) {
        return fail(transException.getErrCode(), transException.getErrMsg(), data, other);
    }

    public static <T> Result<T> fail(TransErrorCode transErrorCode) {
        return fail(transErrorCode.getCode(), transErrorCode.getMsg(), null, null);
    }

    public static <T> Result<T> fail(TransErrorCode transErrorCode, T data) {
        return fail(transErrorCode.getCode(), transErrorCode.getMsg(), data, null);
    }

    public static <T> Result<T> fail(TransErrorCode transErrorCode, T data, T other) {
        return fail(transErrorCode.getCode(), transErrorCode.getMsg(), data, other);
    }

    /**
     * 解析分页插件查出来的Page对象
     * 
     * @param object
     * @return 
     * @return
     */
    public static Object page(Object object) {
        if (object instanceof Page<?>) {
            Page<?> page = (Page<?>)object;
            PageParam whPage = new PageParam();
            whPage.setCurPage(page.getPageNum() != 0 ? page.getPageNum() : 1);
            whPage.setPageSize(page.getPageSize());
            whPage.setTotalPage(page.getPages() != 0 ? page.getPages() : 1);
            whPage.setTotalRow(page.getTotal());
            return whPage;
        }
        return object;
    }

}
